package com.gaattc.controller.mall;

import com.gaattc.model.Result;
import com.gaattc.model.User;
import com.gaattc.model.bo.AdminLoginBO;
import com.gaattc.model.bo.UserChangePwdBO;
import com.gaattc.model.vo.AdminLoginVO;
import com.gaattc.model.vo.UserSignUpVO;
import com.gaattc.service.UserService;
import com.gaattc.service.UserServiceImpl;
import com.gaattc.utils.HttpUtils;
import com.google.gson.Gson;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/api/mall/user/*")
public class UserServlet extends HttpServlet {

    private Gson gson = new Gson();

    private UserService userService = new UserServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/mall/user/", "");
        if ("login".equals(action)) {
            login(request, response);
        } else if ("signup".equals(action)) {
            signUp(request, response);
        } else if ("updatePwd".equals(action)) {
            updatePwd(request, response);
        } else if("updateUserData".equals(action)) {
            updateUserData(request, response);
        } else if("logoutClient".equals(action)) {
            logoutClient(request, response);
        }
    }

    /**
     * 注销登录
     * @param request req
     * @param response resp
     */
    private void logoutClient(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String token = request.getParameter("token");
        request.getSession().invalidate();
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    /**
     * 更改信息
     * @param request req
     * @param response resp
     * @throws IOException io
     */
    private void updateUserData(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String body = HttpUtils.getRequestBody(request);
        User user = gson.fromJson(body, User.class);
        userService.updateUserData(user);
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    /**
     * 修改用户密码
     * @param request req
     * @param response resp
     * @throws IOException io
     */
    private void updatePwd(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        UserChangePwdBO userChangePwdBO = gson.fromJson(requestBody, UserChangePwdBO.class);
        if (!userChangePwdBO.getNewPwd().equals(userChangePwdBO.getConfirmPwd())) {
            response.getWriter().println(gson.toJson(Result.error("新密码不一致,请重输")));
        }
        if (userChangePwdBO.getNewPwd().equals(userChangePwdBO.getOldPwd())) {
            response.getWriter().println(gson.toJson(Result.error("新旧密码一致,请重输")));
        }
        int code = userService.updatePwd(userChangePwdBO);
        if (code == 0) {
            response.getWriter().println(gson.toJson(Result.ok()));
            return;
        }
        if (code == -1) {
            response.getWriter().println(gson.toJson(Result.error("旧密码错误")));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("修改失败")));
    }

    /**
     * 注册用户
     * @param request req
     * @param response resp
     * @throws IOException io
     */
    private void signUp(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        User user = gson.fromJson(requestBody, User.class);
        int code = userService.signUp(user);
        if (code == 0) {
            UserSignUpVO userSignUpVO = new UserSignUpVO(code, user.getEmail(), user.getEmail());
            response.getWriter().println(gson.toJson(Result.ok(userSignUpVO)));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("邮箱重复，请换一个吧")));
    }

    /**
     * 登录用户
     * @param request req
     * @param response resp
     * @throws IOException io
     */
    private void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        AdminLoginBO loginBO = gson.fromJson(requestBody, AdminLoginBO.class);
        if (StringUtils.isEmpty(loginBO.getEmail()) || StringUtils.isEmpty(loginBO.getPwd())) {
            //返回某个响应结果
            response.getWriter().println(gson.toJson(Result.error("参数不能为空")));
            return;
        }
        int code = userService.login(loginBO);
        //返回响应结果
        if (code == 200) {
            response.getWriter().println(gson.toJson(Result.ok(new AdminLoginVO(loginBO.getEmail(), loginBO.getEmail()))));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("确认用户名和密码")));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/mall/user/", "");
        if ("data".equals(action)) {
            data(request, response);
        }
    }

    private void data(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String token = request.getParameter("token");
        User user = userService.data(token);
        response.getWriter().println(gson.toJson(Result.ok(user)));
    }
}
